﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BotonJXC.Util;
using BotonJXC.Entity;
using System.Data;
using NHibernate;
using BotonJXC.DAO;

namespace BotonJXC.BLL
{
    public class OutStocksBLL : BaseBLL<OutStocksDAO,OutStocks,int>
    {
        public OutStocksBLL()
        { 
        }
        PickingsBLL PickingsBLL = null;
        InStocksBll InStocksBll = null;
        WarehouseProductBLL WarehouseProductBLL = null;
        /// <summary>
        /// 保存出库单
        /// </summary>
        /// <param name="orders"></param>
        /// <returns></returns>
        public bool saveOutStocks(OutStocks orders)
        {
            ITransaction trans = HBHelper.BeginTransaction();
            try
            {
                base.Dao.Save(orders);
                trans.Commit();
                return true;
            }
            catch (Exception)
            {
                trans.Rollback();
                throw;

            }
            finally
            {
                HBHelper.Close();
            }
        }

        /// <summary>
        /// 更新出库单
        /// </summary>
        /// <param name="orders"></param>
        /// <returns></returns>
        public bool updateOutStocks(OutStocks OutStocks)
        {
           return updateOutStocks(OutStocks, null, null, false);
        }


        /// <summary>
        /// 报损单审核
        /// </summary>
        /// <param name="orders"></param>
        /// <returns></returns>
        public bool updateOutStocks(OutStocks OutStocks, bool OptQuantity)
        {
            return updateOutStocks(OutStocks, null, null, OptQuantity);
        }
        /// <summary>
        /// 更新出库单
        /// </summary>
        /// <param name="OutStocks">出库单信息</param>
        /// <param name="InStocks">生成入库单</param>
        /// <param name="Pickings">生成拣货单</param>
        /// <param name="OptQuantity">是否更新库存</param>
        /// <returns></returns>
        public bool updateOutStocks(OutStocks OutStocks, InStocks InStocks, Pickings Pickings, bool OptQuantity)
        {
            ITransaction trans = HBHelper.BeginTransaction();
            try
            {

                if (InStocks != null)
                {
                    if (InStocksBll == null) InStocksBll = new InStocksBll();
                    InStocksBll.Save(InStocks);
                    OutStocks.InStocks = InStocks;
                }
                if (Pickings != null)
                {
                    if (PickingsBLL == null) PickingsBLL = new PickingsBLL();
                    PickingsBLL.Save(Pickings);
                }

                base.Dao.Update(OutStocks);
                if (OptQuantity)
                {
                    updateWarehOuseProduct(OutStocks);
                }

                trans.Commit();
                return true;
            }
            catch (Exception)
            {
                trans.Rollback();
                return false;
            }
            finally
            {
                HBHelper.Close();
            }
        }
        public bool updateOutStocks(OutStocks OutStocks,InStocks InStocks, bool OptQuantity)
        {
            return updateOutStocks(OutStocks, InStocks, null, OptQuantity);
        }
        public bool updateOutStocks(OutStocks OutStocks, InStocks InStocks, Pickings Pickings)
        {
            return updateOutStocks(OutStocks, InStocks, Pickings, false);
        }
        /// <summary>
        /// 条件查询出库单
        /// </summary>
        /// <param name="begintime"></param>
        /// <param name="endtime"></param>
        /// <param name="orders"></param>
        /// <param name="start"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public PageModel<OutStocks> findOutStocksByCondition(DateTime begintime, DateTime endtime, OutStocks orders, int start, int count)
        {
            return base.Dao.findOutStocksByCondition(begintime, endtime, orders, start, count);
        }
        public PageModel<OutStocks> findOutStocksByCondition( OutStocks orders, int start, int count)
        {
            return base.Dao.findOutStocksByCondition( orders, start, count);
        }
        /// <summary>
        /// 查询全部出库单
        /// </summary>
        /// <returns></returns>
        public IList<OutStocks> findAllOutStocks()
        {
           return  base.FindAll();
        }

        /// <summary>
        /// 根据ID查询出库单
        /// </summary>
        /// <param name="orderid"></param>
        /// <returns></returns>
        public OutStocks findOutStocksById(string orderid)
        {
            return base.Get(Int32.Parse(orderid));
        }

        /// <summary>
        /// 删除出库单
        /// </summary>
        /// <param name="orders"></param>
        /// <returns></returns>
        public bool DeleteOutStocks(OutStocks OutStocks)
        {
            ITransaction trans = HBHelper.BeginTransaction();
            try
            {
                base.Dao.Delete(OutStocks);
                trans.Commit();
                return true;
            }
            catch (Exception)
            {
                trans.Rollback();
                throw;

            }
            finally
            {
                HBHelper.Close();
            }
        }

        /// <summary>
        /// 根据出库单编号查询订单
        /// </summary>
        /// <param name="orderSn"></param>
        /// <returns></returns>
        public OutStocks findOutStocksBySn(string OutSn, string OutType, string CreatePlace)
        {

            IList<OutStocks> list = base.Dao.findOutStocksBySn(OutSn, OutType, CreatePlace);
            if (list != null && list.Count > 0)
            {
                return list[0];
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 更新库存地货品信息
        /// </summary>
        /// <param name="OutStocks"></param>
        public void updateWarehOuseProduct( OutStocks OutStocks)
        {
            if (WarehouseProductBLL == null) { WarehouseProductBLL = new WarehouseProductBLL(); }
            foreach (OutStocksProducts OutStocksProducts in OutStocks.ProductsList) 
            { 
            WarehouseProductBLL.OptQuantity(OutStocks.OutWarehouse.WarehouseId ,OutStocksProducts.Product.ProductId ,  -OutStocksProducts.RealOutQuantity);
            }
         
        }
    }
}
